package com.pan.admin.module.channel.controller;

import com.gitee.apanlh.util.valid.ValidParam;
import com.pan.admin.common.annotation.Log;
import com.pan.admin.common.core.controller.BaseController;
import com.pan.admin.common.core.domain.AjaxResult;
import com.pan.admin.common.core.page.TableDataInfo;
import com.pan.admin.common.enums.BusinessType;
import com.pan.admin.module.channel.entity.LoanChannel;
import com.pan.admin.module.channel.service.LoanChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 渠道
 * Controller
 *
 * @author ts
 * @date 2023-09-11
 */
@RestController
@RequestMapping("/loanChannel/channel")
public class LoanChannelController extends BaseController {

    @Autowired
    private LoanChannelService loanChannelService;

    /**
     * 查询渠道列表
     */
    @PreAuthorize("@ss.hasPermi('loanChannel:channel:list')")
    @GetMapping("/list")
    public TableDataInfo list(LoanChannel loanChannel) {
        startPage();
        //  如果未选中渠道类型，默认剔除API渠道
        if (ValidParam.isNull(loanChannel.getType())) {
            loanChannel.setSelectApiChannel(false);
        }
        List<LoanChannel> list = loanChannelService.list(loanChannel);
        return getDataTable(list);
    }

    /**
     * 获取渠道详细信息
     */
    @PreAuthorize("@ss.hasPermi('loanChannel:channel:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(loanChannelService.getById(id));
    }

    /**
     * 新增渠道
     */
    @PreAuthorize("@ss.hasPermi('loanChannel:channel:add')")
    @Log(title = "新增渠道", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LoanChannel loanChannel) {
        return toAjax(loanChannelService.insertLoanChannel(loanChannel));
    }

    /**
     * 修改渠道
     */
    @PreAuthorize("@ss.hasPermi('loanChannel:channel:edit')")
    @Log(title = "修改渠道", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LoanChannel loanChannel) {
        return toAjax(loanChannelService.updateLoanChannel(loanChannel));
    }

}
